package com.project;

import com.project.dto.merchantToRider.DetailDto;
import com.project.dto.userToMerchant.OrderDto;
import com.project.dto.userToMerchant.OrderInfoDto;
import com.project.entity.*;
import com.project.service.ISysOrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Service
public class Listener {

    @Autowired
    ISysOrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;


    //监听用户订单mq信道
    @RabbitListener(queues = "UserQueue")
    public void updateNumber(OrderDto orderDto) {
        System.out.println(orderDto);
        if (orderDto == null) {
            return;
        }

        List<SysOrderDetail> orderDetails = new LinkedList<>();

        SysOrder order = new SysOrder();
        //订单编号
        order.setONumber(orderDto.getOrderCode());
        //商铺编号
        order.setStore(new SysStore().setStoreCode(orderDto.getShopCode()));
        //用户编号
        order.setUser(new SysUser().setUserCode(orderDto.getUserCode()));
        //订单送货地址
        order.setOAddress(orderDto.getReceiverAddress());
        //订单状态(商铺未接单)
        order.setOState(0);
        //下单时间
        order.setOTime(orderDto.getOrderTime());
        //订单总金额
        //order.setOMoney(Integer.valueOf(orderDto.getOrderMoney()));
        order.setOMoney(10);
        //收货人电话
        order.setOReceiverPhone(orderDto.getReceiverPhone());
        //订单备注
        order.setODescription(orderDto.getDescription());
        //订单明细集合
        List<OrderInfoDto> list = orderDto.getInfoList();
        for (OrderInfoDto orderInfoDto : list) {
            SysOrderDetail orderDetail = new SysOrderDetail();
            orderDetail.setOrder(new SysOrder().setONumber(String.valueOf(orderInfoDto.getOrderCode())));

            SysProduct product = new SysProduct();
            product.setPkProductid(orderInfoDto.getProductId());
            product.setPName(orderInfoDto.getProductName());
            //product.setPPrice(Integer.valueOf(orderInfoDto.getProductPrice()));
            product.setPPrice(2);
            orderDetail.setProduct(product);

            orderDetail.setNumber(orderInfoDto.getBuyNum());

            orderDetails.add(orderDetail);
        }
        order.setOrderDetails(orderDetails);
        orderService.addOrder(order);
        //新建一个至骑手端的dto
        com.project.dto.merchantToRider.OrderDto dto = new com.project.dto.merchantToRider.OrderDto();
        List<DetailDto> list2 = new ArrayList<>();

        int ra = new Random().nextInt(2);
        if (ra == 1) {
            dto.setArea("A食堂");
        } else {
            dto.setArea("B食堂");
        }
        dto.setCode(orderDto.getOrderCode());
        dto.setCustomerAddress(orderDto.getReceiverAddress());
        dto.setCustomerTel(orderDto.getReceiverPhone());
        dto.setMerchantTel("15827617261");
        int x = new Random().nextInt(3) + 1;
        int y = new Random().nextInt(15) + 1;
        dto.setMerchantAddress(dto.getArea() + (x + "楼" + x + "0" + y));
        dto.setOrderNotes(orderDto.getDescription());
        dto.setStartTime(orderDto.getOrderTime());
        dto.setStatue("可取货");
        dto.setPrice(orderDto.getOrderMoney());
        for (OrderInfoDto orderInfoDto : list) {
            DetailDto detailDto = new DetailDto();
            detailDto.setOrderId(orderInfoDto.getOrderCode());
            detailDto.setProductName(orderInfoDto.getProductName());
            detailDto.setProductNum(String.valueOf(orderInfoDto.getBuyNum()));
            detailDto.setProductPrice(orderInfoDto.getProductPrice());
            list2.add(detailDto);
        }
        dto.setList(list2);
        CorrelationData correlationData = new CorrelationData("");
        rabbitTemplate.convertAndSend("merchantDirectExchange", "Merchant.Queue", dto, correlationData);
    }
}
